/** @file    WoshKioskWindow.cpp
 *  @author  Alessandro Polo
 *  @version $Id: WoshKioskWindow.cpp 2618 2010-06-04 09:48:15Z alex $
 *  @brief
 * File containing methods for the wosh::application::WoshKioskWindow class.
 * The header for this class can be found in WoshKioskWindow.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include "WoshKioskWindow.h"

 #include <core/WoshKernel.h>

 #include <QtGui>


namespace wosh {
  namespace application {
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
WoshKioskWindow* WoshKioskWindow::instance = NULL;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

WoshKioskWindow::WoshKioskWindow() : QGraphicsView()
 {
	this->kManager = NULL;

	this->reallyExiting = false;
	this->reallyExiting = true;
	this->setObjectName(QString::fromUtf8("WoshKioskWindow"));

	//yes this->setWindowFlags( Qt::WindowStaysOnTopHint ); // Qt::FramelessWindowHint | 
	//yes this->setFocusPolicy( Qt::StrongFocus );
	//no this->setAttribute(Qt::WA_QuitOnClose, true);

	Log.setContext("WoshKioskWindow");
	Log.setLevel( LOG_VERBOSE );
	Log.setIndent( 1 );

	WoshKioskWindow::instance = this;

	this->myUri.setKernelLocal();
	this->myUri.clearPathName();
	this->myUri.setName("WoshKioskWindow");


	BusCore.setMessageHandler(this);
	WRESULT bus_core_connected = BusCore.connect( _Bus_Core );
	(void)bus_core_connected;


	setFrameStyle(0);
	setAlignment(Qt::AlignLeft | Qt::AlignTop);
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);


	readSettings();

 }

WoshKioskWindow::~WoshKioskWindow()
 {
	writeSettings();


	WoshKioskWindow::instance = NULL;
 }

void WoshKioskWindow::quitWosh()
 {
	this->reallyExiting = true;
	QApplication::quit();
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////// WORKSPACE

void WoshKioskWindow::setManager( KioskManager* manager )
 {
	if ( manager == NULL )
		return;
	this->kManager = manager;
 }

void WoshKioskWindow::init()
 {
    resize( this->scene()->sceneRect().width(), this->scene()->sceneRect().height() );
	 /*
	this->kManager->addSheet( _SheetDebug_TYPE, true );
	this->kManager->addSheet( _SheetHome_TYPE );

	//TMP
	KioskSheet* Sheet1 = new SheetIWidget( new wosh::gui::FormPlayerAudio() );
	this->kManager->addSheet( Sheet1, false );
//	KioskSheet* Sheet2 = new SheetIWidget( new wosh::gui::FormBuildingManager() );
//	this->kManager->addSheet( Sheet2, false );
	KioskSheet* Sheet3 = new SheetIWidget( new wosh::gui::FormConsole() );
	this->kManager->addSheet( Sheet3, false );

	// load some gadgets
	this->kManager->addGadget( _GadgetStatus_TYPE );
	this->kManager->addGadget( _GadgetModem_TYPE );
//	this->kManager->addGadget( _GadgetMusic_TYPE );
//	this->kManager->addGadget( _GadgetImageViewer_TYPE );
	this->kManager->addGadget( _GadgetCommunicationBox_TYPE );
*/
 }


/////////////////////////////////////////////////////////////////////////////////////////////////// WORKSPACE
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////// MENU HANLDERS


/////////////////////////////////////////////////////////////////////////////////////////////// SIGNAL->EVENT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////// SETTINGS

void WoshKioskWindow::readSettings()
 {
	Log(LOG_VERBOSE, ":readSettings() : Reading.." );
	QSettings settings("OpenSmartHome.com", _APPLICATION_WOSH_KIOSK_NAME);

	int winstate = settings.value("state", (int)Qt::WindowActive).toInt();
	this->setWindowState( (Qt::WindowState)winstate );

	QPoint pos = settings.value("pos", QPoint(50, 50)).toPoint();
	move(pos);
move(QPoint(50, 50));

	QString language = settings.value("language", QLocale::system().name() ).toString();
	loadLanguage(language);

	QString styleName = settings.value("style", "Plastique").toString();
	QString styleSheet = settings.value("styleSheet", "default").toString();
//	this->m_ui->styleCombo->setCurrentIndex(this->m_ui->styleCombo->findText(styleName));
//	this->m_ui->styleSheetCombo->setCurrentIndex(this->m_ui->styleSheetCombo->findText(styleSheet));
styleName = "Plastique";
styleSheet = "default";
	qApp->setStyle(styleName);
	loadStyleSheet(styleSheet);
 }

void WoshKioskWindow::writeSettings()
 {
	Log(LOG_VERBOSE, ":writeSettings() : Writing.." );
	QSettings settings("OpenSmartHome.com", _APPLICATION_WOSH_KIOSK_NAME);

	settings.setValue("state", (int)this->windowState() );
	if ( this->windowState() != Qt::WindowMaximized && this->windowState() != Qt::WindowFullScreen ) {
		settings.setValue("pos", pos());
	 }

	settings.setValue("style", qApp->style()->objectName() );
	settings.setValue("styleSheet", qApp->styleSheet() );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////// SETTINGS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void WoshKioskWindow::changeEvent(QEvent *e) {
	QGraphicsView::changeEvent(e);
	switch (e->type()) {
		case QEvent::LanguageChange:
			//refreshInfo(); // refresh title
			break;
		default:
			break;
	 }
 }

void WoshKioskWindow::closeEvent(QCloseEvent *event) {
	if ( this->reallyExiting )
		return;
	//The program will keep running in the system tray.
	hide();
	event->ignore();
 }

	//	void resizeEvent( QResizeEvent* r_event ) {
	//		QGraphicsView::resizeEvent(r_event);
	//		if ( this->scene() != NULL )
	//			fitInView(this->scene()->sceneRect(), Qt::IgnoreAspectRatio );
	//	 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void WoshKioskWindow::loadStyleSheet(const QString &sheetName)
 {
QFile file("H:\\WOSHs\\Win32\\src\\apps\\woshkiosk\\styles\\" + sheetName.toLower() +  ".qss");

//	QFile file(":/qss/" + sheetName.toLower() + ".qss");
	file.open(QFile::ReadOnly);
	QString styleSheet = QLatin1String(file.readAll());
	qApp->setStyleSheet(styleSheet);
 }

void WoshKioskWindow::loadLanguage(const QString& language)
 {
	if ( language.size() == 0 ) return;

	QTranslator* qtTranslator = new QTranslator();
	QString trQt = "qt_" + language;
	bool loadedQt = qtTranslator->load(trQt, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
	if ( loadedQt )
		qApp->installTranslator(qtTranslator);
//	else
//		QMessageBox::critical(this, this->windowTitle(), QString("Error occurred while loading Translation file [%1].").arg(trQt) );

	QTranslator* woshWidgetTranslator = new QTranslator();
	QString trWw = ":/translations/widgets."+language+".qm";
	bool loadedWw = woshWidgetTranslator->load( trWw );
	if ( loadedWw )
		qApp->installTranslator(woshWidgetTranslator);
	else
		QMessageBox::critical(this, this->windowTitle(), QString("Error occurred while loading Translation file [%1].").arg(trWw) );

	QTranslator* myappTranslator = new QTranslator();
	QString trMy = ":/translations/"+language+".qm";
	bool loadedMy = myappTranslator->load( trMy );
	if ( loadedMy )
		qApp->installTranslator(myappTranslator);
	else
		QMessageBox::critical(this, this->windowTitle(), QString("Error occurred while loading Translation file [%1].").arg(trMy) );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void WoshKioskWindow::busMessage( const Message&, const Bus* )
 {


 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace application
}; // namespace wosh
